package com.example.nna.voz;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.nna.main.R;
import com.example.nna.util.UserInfo;

public class pageUserCP extends Activity{
	private ListView mMainList;
	private listViewCustomSubscribe adapter;
	private ParserUserCP mParser;
	private Activity mActivity;
	private Context mContext;
	private ProgressDialog progressDialog;
	private UserInfo mUser;
	private TextView m_TextLogin;
	private PaserTask mTask;
	private TextView m_Detail;
	private int stateConenct;
	private String mUrl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page2);
		mMainList = (ListView)findViewById(R.id.page2);
		mActivity = this;
		mContext = this;
		mUser = new UserInfo();
		progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Loading data......");
		m_Detail = (TextView)findViewById(R.id.Page2Detail);
		mUrl =  "http://vozforums.com/subscription.php?do=viewsubscription";
		mHandler.obtainMessage(Voz.MSG_SUBSCRIBE,mUrl).sendToTarget();
	}
	private void NextClick(){
		mTask.cancel(true);
		if(mParser.Urlnext != null){
			mUrl = mParser.Urlnext;
			mHandler.obtainMessage(Voz.MSG_SUBSCRIBE).sendToTarget();
		}else{
			Toast.makeText(mActivity, "Cannot go to Next", Toast.LENGTH_LONG).show();
		}
	}
	
	private void PrevClick(){
		mTask.cancel(true);
		if(mParser.Urlprev != null){
			mUrl = mParser.Urlprev;
			mHandler.obtainMessage(Voz.MSG_SUBSCRIBE).sendToTarget();
		}else{
			Toast.makeText(mActivity, "Cannot go to Previous.", Toast.LENGTH_LONG).show();
		}
	}
	private void FirstClick(){

		mTask.cancel(true);
		if(mParser.Urlfirst!=null){
			mUrl = mParser.Urlfirst;
			mHandler.obtainMessage(Voz.MSG_SUBSCRIBE).sendToTarget();
		}else Toast.makeText(mActivity, "Cannot go to first! ", Toast.LENGTH_LONG).show();

	}
	private void GotoClick(){
		mTask.cancel(true);
		mHandler.obtainMessage(Voz.MSG_SUBSCRIBE).sendToTarget();
	}
	private void LastClick(){

		mTask.cancel(true);
		
		if(mParser.Urllast != null){
			mUrl = mParser.Urllast;
			mHandler.obtainMessage(Voz.MSG_SUBSCRIBE).sendToTarget();
		}else{
			Toast.makeText(mActivity, "Cannot go to Last", Toast.LENGTH_LONG).show();
		}
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("nna","Page2Start");
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//mTask.cancel(true);
		Log.d("nna","Page2Stop");
	}
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d("nna","Page2Resume");
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d("nna","Page2Detroy");
	}
	private class PaserTask extends AsyncTask<Integer, Void, Void>{

		@Override
		protected Void doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			switch (params[0]) {
			case Voz.PASER_SUBSCRIBE:
				stateConenct = mParser.parser();
				if(stateConenct==-1){
					Log.d("nna", "loi");
				}
				break;
			default:
				break;
			}
			return null;
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			progressDialog.show();
			super.onPreExecute();
		}
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			progressDialog.dismiss();
			m_Detail.setText(mParser.getIndexPage());
			adapter.notifyDataSetChanged();
			
		}
     }
	 public final Handler mHandler = new Handler(){
		 	Integer pos;
	    	@Override
	    	public void handleMessage(Message msg) {
	    		switch (msg.what) {
	    		case Voz.MSG_SUBSCRIBE:
	    			mParser = new ParserUserCP(mUrl);
		  		    adapter = new listViewCustomSubscribe(mActivity, mHandler, mParser.getContain());
		  		    adapter.setSize(Voz.mTextSize);
		  		    mMainList.setAdapter(adapter);
		  		    mTask = new PaserTask();
		  		    mTask.execute(Voz.PASER_SUBSCRIBE);
	    			break;
	    		case Voz.MSG_GOTO:
	    			switch ((Integer)msg.obj) {
					case 0:	GotoClick();break;
					case 1:	FirstClick();break;
					case 2:	PrevClick(); break;
					case 3:	NextClick();break;
					case 4:	LastClick();break;
					default:break;
					}
	    			break;
	    		 case Voz.MSG_LIKEPAGE:
	    			 Bundle extras = new Bundle();
	    			 extras.putString("url",Voz.URL+(String)msg.obj);
				 	 extras.putString("forum","test");
	    			 Intent i = new Intent(mContext,page3.class );
	 				 i.putExtras(extras);
	 				 startActivity(i);
	 		    	break; 
	    		}
	    }
	 };
}
